<script src='../functions.js'></script>
<script src='../dom.js'></script>
<script src='../dom.event.js'></script>
<script src='../utils.js'></script>
<script src='../debug.js'></script>
<script src='../liveui.js'></script>

<script src='/protolex/lex3.js'></script>
<script src='/protolex/lex3.extra.js'></script>

<body>

<script>

(function(){

var print_out = [];
var print = function(){
	var x = arguments.length > 1 ? sprintf.apply(this, arguments) : arguments[0];
	print_out[print_out.length] = x;
};
window.setTimeout(function(){
	var elem = document.createElement('pre');
	elem.innerHTML = print_out.join('');
	document.body.appendChild(elem);
	print_out = [];
}, 100);



var buf = [];

var no_regex = false;

mklex({
	INITIAL: [
		[/<\/?[a-z][^<>]*>/, function(yytext){ print('<span style="background-color:cyan;font-weight:bold;">%s</span>', html_escape(yytext)); }],
		[/\/\/[^\n]*/, function(yytext){ print('<span style="color:#773;font-style:italic">%s</span>', html_escape(yytext)); }],
		['/*', function(yytext){ buf = []; BEGIN('block_comment') }],		
		['\'', function(yytext){ buf = []; BEGIN('q_string') }],

		['/',
			function(yytext){
				if (no_regex) {
					no_regex = false;
					print('%s', html_escape(yytext));
				} else {
					buf = [];
					BEGIN('regexp');
				}
			}],

		['\"', function(yytext){ buf = []; BEGIN('dq_string') }],
		[/\./, function(yytext){ print('<span style="font-weight:bold;">%s</span>', yytext); }],

		[/\{|\}|\(|\)|\[|\]/,
			function(yytext){
				switch (yytext) {
					case '{': case '(': case '[': no_regex = false; break;
					default: no_regex = true;
				}
				print('<span style="zfont-weight:bold;color:#599;">%s</span>', yytext);
			}],

		[/\+\+|\-\-|(=|\\!)?==|[\+\-\*\/]=|&&|\|\||!/,
			function(yytext){ 
				no_regex = false;
				print('<span style="color:green">%s</span>', yytext);
			}],

		[/undefined|arguments/,
			function(yytext){ print('<span style="color:magenta">%s</span>', yytext); }],

		[/\?|:|,|=|var|function|for|this|in|if|true|false|return|continue|null|while|break|switch|case|default|else|new|delete/, 
			function(yytext){
				no_regex = false;
				print('<span style="font-weight:bold">%s</span>', yytext);
			}],

		[/[a-zA-Z$_][a-zA-Z$_0-9]*/, 
			function(yytext){
				no_regex = true;
				print('%s', yytext);
			}],

		[/[0-9]+/,
			function(yytext){
				no_regex = true;
				print('<span style="color:green">%s</span>', yytext);
			}],
		
		[/</, function(yytext){ print('&lt;'); }],
		[/>/, function(yytext){ print('&gt;'); }],
		[/./, function(yytext){ print(yytext); }],
	],

	after_ident: [
		[/</, function(yytext){ print('&lt;'); BEGIN('INITIAL'); }],
		[/>/, function(yytext){ print('&gt;'); BEGIN('INITIAL'); }],
		[/./, function(yytext){ print(yytext); BEGIN('INITIAL'); }],
	],

	regexp: [
		[/\\./, function(yytext){ buf[buf.length] = sprintf('<span style="color:red;font-weight:bold">%s</span>', yytext); }],

		[/\/[igm]?/, function(yytext){ print('<span style="color:red;font-weight:bold">/%s/</span>', buf.join(''));  BEGIN('INITIAL') }],
		[/</, function(yytext){ buf[buf.length] = '&lt;' }],
		[/>/, function(yytext){ buf[buf.length] = '&gt;' }],
		[/[^\n]/, function(yytext){ buf[buf.length] = yytext; }],
	],


	dq_string: [
		[/\\./, function(yytext){ buf[buf.length] = sprintf('<span style="color:red;font-weight:bold">%s</span>', yytext); }],
		['\"', function(yytext){ print('<span style="color:#00f;font-weight:bold">\"%s\"</span>', buf.join(''));  BEGIN('INITIAL') }],

		[/</, function(yytext){ buf[buf.length] = '&lt;' }],
		[/>/, function(yytext){ buf[buf.length] = '&gt;' }],
		[/./, function(yytext){ buf[buf.length] = yytext; }],
	],
	
	q_string: [
		[/\\./, function(yytext){ buf[buf.length] = sprintf('<span style="color:red;font-weight:bold">%s</span>', yytext); }],
		['\'', function(yytext){ print('<span style="color:#00f;font-weight:bold">\'%s\'</span>', buf.join(''));  BEGIN('INITIAL') }],

		[/</, function(yytext){ buf[buf.length] = '&lt;' }],
		[/>/, function(yytext){ buf[buf.length] = '&gt;' }],
		[/./, function(yytext){ buf[buf.length] = yytext; }],
	],
	
	block_comment: [
		['*/', function(yytext){ print('<span style="color:#773;font-style:italic">/*%s*/</span>', buf.join(''));  BEGIN('INITIAL') }],
		
		[/</, function(yytext){ buf[buf.length] = '&lt;' }],
		[/>/, function(yytext){ buf[buf.length] = '&gt;' }],
		[/./, function(yytext){ buf[buf.length] = yytext; }],
	],
});

var text = load_file('jquery.src.js');
var t0 = new Date;
yylex(text);
var t1 = new Date;
alert(t1-t0 + ' ms.');

})();

</script>